/* This code shows how the next process to be scheduled 
   is chosen in our system.
   For this sketch, the ready queue is assumed not empty
   and running not NULL.
 */

PCB* choose_next_process(){
	/* gets the highest priority process */
	PCB* first = get_first();
	if(round_robin){
		/* scheduler in RR mode */
		/* gets the number of processes involved in the round */
		int count = in_ready_queue[running->priority];
		/* does the RR mode need to continue? Two cases:
			- count < 2
			- a process (first) with a higher priority has arrived
		*/
		if(count < 2 || first->priority > running->priority){
			round_robin = 0;
		}else{
			/* RR still in use */
			/* get the index of the next process to run */
			round_robin_index = (round_robin_index >= count?
					0:(round_robin_index +1)%count);
			/* round_robin_index now indicates the next process to run */
			return get_by_index(round_robin_index); 
		}
	}	
	/* the RR mode has been deactivated or is not used,
	   we must run the process with highest priority, that is first */
	/* is there other processes with the highest priority? */
	if(in_ready_queue[first->priority] > 1){
		/* Round Robin mode */
		round_robin = 1;
		/* this first process in the queue is executing */
		round_robin_index = 0;
	}
	return first;
}
